#include "db.h"
#include <Poco/Data/SessionPool.h>
#include <stdexcept>

using namespace servercom;

std::unique_ptr<Poco::Data::SessionPool> Database::session_pool_;

void Database::initialize(const std::string& db_type,
                         const std::string& connection_string,
                         int min_sessions,
                         int max_sessions) {
    if (session_pool_) {
        throw std::runtime_error("Database already initialized");
    }

    try {
        session_pool_ = std::make_unique<Poco::Data::SessionPool>(
            db_type, connection_string, min_sessions, max_sessions);
    } catch (const Poco::Exception& e) {
        throw std::runtime_error("Failed to initialize database: " + e.displayText());
    }
}

Poco::Data::Session Database::getSession() {
    if (!session_pool_) {
        throw std::runtime_error("Database not initialized");
    }
    return session_pool_->get();
}

void Database::shutdown() {
    if (session_pool_) {
        session_pool_.reset();
    }
}